    /**
     * 获得base64
     * @param {Object} o
     * @param {Number} [o.width] 图片需要压缩的宽度
     * @param {Number} [o.height] 图片需要压缩的高度，为空则会跟随宽度调整
     * @param {Number} [o.quality=0.8] 压缩质量，不压缩为1
     * @param {Number} [o.mixsize] 上传图片大小限制
     * @param {Number} [o.type] 上传图片格式限制
     * @param {Function} [o.before(blob)] 处理前函数,this指向的是input:file
     * @param {Function} o.success(obj) 处理后函数
     * @param {Function} o.error(obj) 处理后函数
     * @example
     *
     * $('#test').UploadImg({
            url : '/upload.php',
            width : '320',
            //height : '200',
            quality : '0.8', //压缩率，默认值为0.8
            // 如果quality是1 宽和高都未设定 则上传原图
            maxsize : '1',
            //type : 'image/png,image/jpg,image/jpeg,image/pjpeg,image/gif,image/bmp,image/x-png',
            before : function(blob){
                //上传前返回图片blob
                $('#img').attr('src',blob);
            },
            error : function(res){
                //上传出错处理
                $('#img').attr('src','');
                $('#error').html(res);
            },
            success : function(res){
                //上传成功处理
                $('#imgurl').val(res);
            }
        });
     */


$.fn.UploadImg = function(o){
    
    var config = {
        type : 'image/png,image/jpg,image/jpeg,image/pjpeg,image/gif,image/bmp,image/x-png',
    };
    
    $.extend( o , config );
	this.change(function(){
        tip_loading.show();
        //$('#input_upload_file').hide();
		var file = this.files['0'];

		if( file.size && file.size > o.maxsize ){
			tips('大小超过限制' + Math.round ( o.maxsize / 1048576 , 1) + 'M' );
			this.value='';
		} else if( o.type && o.type.indexOf(file.type) < 0){
			tips('格式不正确');
			this.value='';
		} else{
            var reader = new FileReader();
            reader.onload = function(e){
                var blob = e.target.result;
                //o.before(blob);
                _compress(blob,file);
            }
            reader.readAsDataURL( file );
            this.value='';
            return ;
		}
        tip_loading.hide();
	});

    //图片压缩
	function _compress(blob,file){
		var img = new Image();

		img.onload = function(){

            if ( o.width && o.width > this.width ) {
                var info = '图片宽度不能小于' + o.width + 'px';
                tips( info );
                o.error( {status: false , info : info });
                return ;
            }
            
            if ( o.height && o.height > this.height ) {
                var info = '图片高度不能小于' + o.width + 'px';
                tips( info );
                o.error( {status: false , info : info });
                return ;    
            }

			var canvas = document.createElement('canvas');
			var ctx = canvas.getContext('2d');
			if(!o.width && !o.height && o.quality == 1){
				var w = this.width;
				var h = this.height;
			}else{
				var w = o.width || this.width;
				var h = o.height || w/this.width*this.height;
			}
			$(canvas).attr({width : w, height : h});
            
			ctx.drawImage(this, 0, 0, w, h);
			var base64 = canvas.toDataURL( file.type, o.quality || 0.8 );
            
            
            /*
			if( navigator.userAgent.match(/iphone/i) ) {
				var mpImg = new MegaPixImage(img);
				mpImg.render(canvas, { maxWidth: w, maxHeight: h, quality: o.quality || 0.8, orientation: 6 });
				base64 = canvas.toDataURL(file.type, o.quality || 0.8 );
			}*/


			// 修复android
			if( navigator.userAgent.match(/Android/i) ) {
                try {
                    var encoder = new JPEGEncoder();
                    base64 = encoder.encode(ctx.getImageData(0,0,w,h), o.quality * 100 || 80 );
                } catch (e) {
                    alert(e.message);
                    alert(e.description)
                    alert(e.number)
                    alert(e.name)
                    throw new Error(10,"asdasdasd")
                } 

			}

			_ajaximg(base64,file.type);
		};

        img.src = blob;
        tip_loading.hide();
	}

	function _ajaximg(base64,type,file){
		$.post(o.url,{base64:base64,type:type},function(res){
            tip_loading.hide();
			if( !res.status ){
				o.error(res);
			}else{
				o.success(res);
			}
		});
	}
};

